#!/bin/bash

##### SETTINGS #####

ftphost="example.com"
ftpuser="username"
ftppass="password"
ftppath="/public_html/wordpress"
wpurl="http://example.com/wordpress"

##### STOP EDITING HERE #####

# Make a working directory
echo Making a temporary directory to work in.
workingdir=`mktemp -d`
cd $workingdir

# Initial setup
function end {
	echo $@
	read -p "(push enter to exit)"
	exit
}
function up_dev {
	# Check the current repository revision
	echo "open $ftphost
user $ftpuser $ftppass
cd $ftppath
get .llama-update-revision
bye" | ftp -ni &> /dev/null
	savedrev=`readlink -f ./.llama-update-revision`
	read oldrev <<< `cat $savedrev`
	if [[ ! $oldrev ]]
	then oldrev=0 # Unfortunately, we'll need to get everything and waste a little bit of bandwidth.
	fi

	echo Checking for latest WordPress version...
	newrev=`svn info "http://svn.automattic.com/wordpress/trunk" | awk '/^Revision:/ {print $2}'`
	echo "Latest WordPress version is $newrev.  Currently at $oldrev."
	echo $newrev > $savedrev

	# Check if an update really is necessary
	if [[ $oldrev -ge $newrev ]]
	then end "No upgrade needed, already at revision $oldrev"
	fi

	# Get changed files
	echo Obtaining changed files from revision $oldrev to revision $newrev.
	curl --silent "http://trac.wordpress.org/changeset?format=zip&old_path=trunk&old="${oldrev}"&new_path=trunk" > wordpress.zip
	unzip wordpress.zip > /dev/null
	rm wordpress.zip
	mkdir -p wp
	mv $savedrev wp/
	mv trunk wp
	cd wp
	echo Changed files successfully obtained.

	finish
}
function upgrade {
	echo "open $ftphost
user $ftpuser $ftppass
cd $ftppath/wp-includes
get version.php
bye" | ftp -ni &> /dev/null
	oldtag=`awk '/^\\$wp_version/\' version.php`
	oldtag=${oldtag:15:${#oldtag}-17}
	curl --silent http://api.wordpress.org/core/version-check/1.3/?version=$oldtag > ${workingdir}/upinfo
	if [[ `awk 'FNR == 1' ${workingdir}/upinfo` = "development" ]]
	then up_dev
		return
	elif [[ `awk 'FNR == 1' ${workingdir}/upinfo` = "latest" ]]
	then end "You are already up to date."
	fi
	newtag=`awk 'FNR == 4' ${workingdir}/upinfo`
	# Get changed files
	echo Obtaining changed files from WordPress $oldtag to WordPress $newtag.
	curl --silent "http://trac.wordpress.org/changeset?format=zip&old_path=tags%2F"${oldtag}"&new_path=tags%2F"${newtag} > wordpress.zip
	unzip wordpress.zip > /dev/null
	rm wordpress.zip
	mv tags/${newtag} wp
	mkdir -p wp
	cd wp
	echo Changed files successfully obtained.
	finish
}
function addwpdir {
	if [[ -d $@ ]]
	then echo "lcd $@
cd $@
mput *
lcd ..
cd .." >> ${workingdir}/ftp
fi
}
function finish {
	# Make the FTP command list
	echo Generating FTP command list.
	echo "open $ftphost
	user $ftpuser $ftppass
	binary
	cd $ftppath
	mput *" > $workingdir/ftp
	for cwpdir in `ls`
	do addwpdir $cwpdir
	done
	echo "bye" >> ${workingdir}/ftp

	# Execute the FTP command list
	echo Executing FTP command list
	ftp -ni < ${workingdir}/ftp &> /dev/null

	# Run the upgrade script (if needed)
	if [[ -e ./wp-admin/includes/upgrade.php ]]
	then echo Running upgrade script
		curl --silent $wpurl/wp-admin/upgrade.php?step=1 > /dev/null
	fi

	# Cleanup
	echo Cleaning up.
	rm -fr $workingdir &> /dev/null
	rmdir $workingdir &> /dev/null

	# We're done!
	end "Successfully updated!"
}

# Execute our code
upgrade